// pages/pay/pay.js
var app ;
var url;
var headers;
Page({
  data:{
    orderMes:{},
    url:"https://app1.sztravel.com.cn/file/weixinapp/img"
  },
  test(e){
    console.log(e);
    //  wx.login({
    //   success: function (res) {
    //     console.log(res);
    //     wx.request({
    //       //获取openid接口
    //       url: 'httpss://api.weixin.qq.com/sns/jscode2session',
    //       data: {
    //         appid: app.globalData.wxMes.appId,
    //         secret: app.globalData.wxMes.secret,
    //         js_code: res.code,
    //         grant_type: 'authorization_code'
    //       },
    //       method: 'GET',
    //       success: function (openres) {
    //         console.log(openres.data)
    //         OPEN_ID = openres.data.openid;//获取到的openid
    //         SESSION_KEY = openres.data.session_key;//获取到session_key
    //         console.log(OPEN_ID)
    //         console.log(SESSION_KEY)
    //         that.setData({
    //           OPENID: openres.data.openid
    //         })
    //       }
    //     })
    //   }
    // })
  },
  payNow:function(){
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: url + 'api/charge_order',
            header: headers,
            method: 'POST',
            data: {
              code: res.code,
              "order_no": that.data.orderMes.order_no,
              "type": "wx",
              "pay_price": parseFloat(that.data.orderMes.total_price).toFixed(2),
              "gzh": "2"
            },
            success: function (data) {
              console.log(data);
            wx.requestPayment({
              timeStamp: ''+data.data.data.msg.timestamp,
              nonceStr: data.data.data.msg.nonce_str,
              package: data.data.data.msg.package,
              signType: data.data.data.msg.signtype,
              paySign: data.data.data.msg.sign
            })
            },
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成

  },
  onShow:function(){
    app = getApp();
    url = app.globalData.url;   
    var that=this
    // 页面显示
    headers = app.globalData.headers;
    try{
      var orderMes = JSON.parse(wx.getStorageSync('orderMes'));
      that.setData({
        orderMes: orderMes
      })
      console.log(that.data)
    }catch(e){

    };
   
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})